<template>
  <a-spin :spinning="confirmLoading">
    <ants-form-container :disabled="formDisabled">
      <a-form slot="detail" :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="角色名称" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-decorator.trim="['roleName', validatorRules.roleName]" placeholder="请输入角色名称" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="角色编码" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-decorator.trim="['roleCode', validatorRules.roleCode]" placeholder="请输入角色编码" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="描述" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-textarea v-decorator="['description', validatorRules.description]" :rows="4" placeholder="请输入描述" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </ants-form-container>
  </a-spin>
</template>

<script>
import AntsFormContainer from '@/components/ants/AntsFormContainer'
import pick from 'lodash.pick'
import store from '@/store'
import { duplicateCheck } from '@/api/api'

export default {
  name: 'RoleForm',
  components: {
    AntsFormContainer
  },
  props: {
    // 流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    // 表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 }
      },
      validatorRules: {
        roleName: {
          rules: [
            { required: true, message: '请输入角色名称!' },
            { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
          ]
        },
        roleCode: {
          rules: [
            { required: true, message: '请输入角色名称!' },
            { min: 0, max: 64, message: '长度不超过 64 个字符', trigger: 'blur' },
            { validator: this.validateRoleCode }
          ]
        },
        description: {
          rules: [
            { min: 0, max: 126, message: '长度不超过 126 个字符', trigger: 'blur' }
          ]
        }
      },
      url: {
        add: '/sys/role/add',
        edit: '/sys/role/edit'
      },
      confirmLoading: false,
      confirmDirty: false,
      userId: ''
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    }
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.userId = record.id
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'roleName', 'roleCode', 'description'))
      })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          const formData = Object.assign(this.model, values)

          // eslint-disable-next-line no-unused-vars
          var url = ''
          var method = ''
          if (!this.model.id) {
            url = that.url.add
            method = 'post'
          } else {
            url = that.url.edit
            method = 'put'
          }
          store.dispatch('user/addOrUpdateUser', {
            url: url,
            method: method,
            params: formData
          }).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    },
    validateRoleCode(rule, value, callback) {
      if (/[\u4E00-\u9FA5]/g.test(value)) {
        callback('角色编码不可输入汉字!')
      } else {
        var params = {
          tableName: 'sys_role',
          fieldName: 'role_code',
          fieldVal: value,
          dataId: this.model.id
        }
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback(res.message)
          }
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
